package com.tanhua.dubbo.mongo.api;

import com.tanhua.model.mongo.Questions;
import com.tanhua.model.mongo.TestSoul;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class QuestionsApiImpl implements QuestionsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据问卷id查询所有题目的集合
    public Map<ObjectId, List<Questions>> findQuestions(List<ObjectId> ids) {
        //构建一盒key为问卷id,value为 试题集合的map
        Map<ObjectId, List<Questions>> map=new HashMap<>();

        //循环,根据问卷id查询所对应的所有试题集合
        for (ObjectId id : ids) {

            Criteria criteria = Criteria.where("testSoulId").is(id);
            Query query = Query.query(criteria);
            List<Questions> questions = mongoTemplate.find(query, Questions.class);
            map.put(id,questions);

        }
        return map;
    }

    //根据questionId 查询 问卷id
    public ObjectId findByQuestionId(String questionId) {
        Criteria criteria = Criteria.where("id").is(questionId);
        Query query = Query.query(criteria);
        Questions questions = mongoTemplate.findOne(query, Questions.class);

        return questions.getTestSoulId();
    }
}
